/**
 * Collaborative Story Development Web Application (CSDApp) 
 * Copyright Anton Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.util.HashMap;
import java.util.Map;

/**
 * This class represents an access control. The access control is determined by
 * a Role indicating who can preform the Action on the specific Resource type
 * filtered by if they own the resource of the resource type or not.
 *
 * @author Anton Strack
 */
public class Acl {

    /**
     * The role type of the user the access control references.
     */
    private int roleId;
    /**
     * The action type to be preformed by the role.
     */
    private int actionId;
    /**
     * The resource type of the resource to preform the action on.
     */
    private int resourceTypeId;
    /**
     * The ownership status of the resource of the resource type to the user
     * represented by the role.
     */
    private int ownership;
    /**
     * The value indicating if the user matching the above four conditions is
     * granted access.
     */
    private boolean access;
    /**
     * The name of combined ids as a list in the array.
     */
    private Map<String, String> idNames;

    public Acl() {
        this.idNames = new HashMap<String, String>();
    }

    public Acl(int roleId, int actionId, int resourceTypeId, int ownership, boolean access) {
        this();
        this.roleId = roleId;
        this.actionId = actionId;
        this.resourceTypeId = resourceTypeId;
        this.ownership = ownership;
        this.access = access;
    }

    /**
     * @return the roleId
     */
    public int getRoleId() {
        return roleId;
    }

    /**
     * @param roleId the roleId to set
     */
    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    /**
     * @return the actionId
     */
    public int getActionId() {
        return actionId;
    }

    /**
     * @param actionId the actionId to set
     */
    public void setActionId(int actionId) {
        this.actionId = actionId;
    }

    /**
     * @return the resourceTypeId
     */
    public int getResourceTypeId() {
        return resourceTypeId;
    }

    /**
     * @param resourceTypeId the resourceTypeId to set
     */
    public void setResourceTypeId(int resourceTypeId) {
        this.resourceTypeId = resourceTypeId;
    }

    /**
     * @return the ownership
     */
    public int getOwnership() {
        return ownership;
    }

    /**
     * @param ownership the ownership to set
     */
    public void setOwnership(int ownership) {
        this.ownership = ownership;
    }

    /**
     * @return the access
     */
    public boolean getAccess() {
        return access;
    }

    /**
     * @param access the access to set
     */
    public void setAccess(boolean access) {
        this.access = access;
    }

    public String getIdName(String nameOf) {
        if (this.idNames.containsKey(nameOf)) {
            return this.idNames.get(nameOf);
        }
        return "";
    }

    public void setIdNames(String nameFor, String name) {
        this.idNames.put(nameFor, name);
    }

    @Override
    public String toString() {

        StringBuilder sb = new StringBuilder();
        if (this.idNames.isEmpty() == false) {
            for (String value : this.idNames.values()) {
                sb.append(", ").append(value);
            }
        }
        return "Acl{" + "roleId=" + roleId + ", actionId=" + actionId + ", resourceTypeId=" + resourceTypeId + ", ownership=" + ownership + ", access=" + access + "} " + sb.toString();
    }
}
